//
//  HMHUDManager.m
//  项目的登录注册模块的封装
//
//  Created by 孙鹏飞 on 2017/3/20.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMHUDManager.h"

@implementation HMHUDManager

#define Img(name)         [UIImage imageNamed : name]
#define StrWithFormat(fmt, ...) [NSString stringWithFormat : fmt, ## __VA_ARGS__]

// UIColor
#define UIColorFromRGB(r,g,b)         UIColorFromRGBA(r,g,b,1.0)
#define UIColorFromRGBA(r,g,b,a)      [UIColor colorWithRed : r/255.0 green : g/255.0 blue : b/255.0 alpha : a]
#define UIColorFromRGBAVaule(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:a]
#define UIColorFromRGBVaule(rgbValue) UIColorFromRGBAVaule(rgbValue,1.0)

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_WIDTH_RATE (SCREEN_MIN_LENGTH/320.0)
#define SCREEN_WIDTH_RATIO (SCREEN_MIN_LENGTH/375.0)

/** 取消所有HUD */
+ (void)hideAllHUDs
{
    UIView * window = [[[UIApplication sharedApplication] delegate] window];
    [MBProgressHUD hideAllHUDsForView:window animated:YES];
}

/** 显示进度HUD */
+ (MBProgressHUD *)showHUD
{
    [HMHUDManager hideAllHUDs];
    UIView * window = [[[UIApplication sharedApplication] delegate] window];
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.color = [UIColor clearColor];
    
    hud.customView = [self loadingAnimationViewWithFrame:CGRectMake(0, 0, 66, 42)];
    return hud;
}

/** 显示自动消失的文字HUD */
+ (MBProgressHUD *)showHUDWithText:(NSString *)text
{
    if (!text.length) {
        return nil;
    }
    [HMHUDManager hideAllHUDs];
    return [self showHUDWithText:text autoHide:YES];
}

/** 显示成功／失败HUD */
+ (MBProgressHUD *)showHUDWithText:(NSString *)text success:(BOOL)success
{
    [HMHUDManager hideAllHUDs];
    UIImage * image;
    if (success) {
        image = Img(@"hud_success");
    } else {
        image = Img(@"hud_failure");
    }
    return [self showHUDWithText:text image:image];
}

/** 显示图文HUD */
+ (MBProgressHUD *)showHUDWithText:(NSString *)text image:(UIImage *)image
{
    return [self showHUDWithText:text image:image autoHide:YES];
}

// 显示文字HUD，不消失
+ (MBProgressHUD *)showHUDWithText:(NSString *)text autoHide:(BOOL)autoHide
{
    return [self showHUDWithText:text image:nil autoHide:autoHide];
}

// 显示图文HUD，不消失
+ (MBProgressHUD *)showHUDWithText:(NSString *)text image:(UIImage *)image autoHide:(BOOL)autoHide
{
    [HMHUDManager hideAllHUDs];
    
    //    UIViewController *topVC = [[RIVNavigationManager shared] getCurrentVC];
    UIView *window = [[[UIApplication sharedApplication] delegate] window];
    //    if (!topVC.view) {
    //        return nil;
    //    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:NO];
    hud.removeFromSuperViewOnHide = YES;
    
    if (image) {
        hud.mode = MBProgressHUDModeCustomView;
        
        UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
        imgView.frame = CGRectMake(0, 0, 28, 28);
        hud.customView = imgView;
        
    } else {
        hud.mode = MBProgressHUDModeText;
        hud.color = kMainColor;
        hud.labelColor = [UIColor whiteColor];
        //    hud.labelText = text;
        //    hud.labelFont = [UIFont systemFontOfSize:14];
        //        hud.detailsLabelText = text;
        //        hud.detailsLabelFont = [UIFont systemFontOfSize:14];
    }
    
    hud.detailsLabelText = text;
    hud.detailsLabelFont = [UIFont systemFontOfSize:14];
    
    if (autoHide) {
        [hud hide:YES afterDelay:1.0];
    }
    
    return hud;
}

#pragma mark - View Util

// 加载中页面
+ (UIView *)loadingDataView
{
    CGFloat imgWidth = 66;
    CGFloat imgHeight = 42;
    
    //    CGFloat imgWidth = 30;
    //    CGFloat imgHeight = 30;
    
    CGFloat titleHeight = 30*SCREEN_WIDTH_RATE;
    CGFloat loadingViewWidth = SCREEN_WIDTH;
    
    UIView *loadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, loadingViewWidth, imgHeight + titleHeight)];
    loadingView.backgroundColor = [UIColor clearColor];
    
    // 旋转
    UIImageView *loadingAnimationView = [self loadingAnimationViewWithFrame:CGRectMake(loadingViewWidth/2 - imgHeight/2, 0, imgWidth, imgHeight)];
    
    // 加载中
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(loadingAnimationView.frame), loadingViewWidth, titleHeight)];
    titleLabel.textColor = UIColorFromRGBVaule(0xD8D8D8);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"正在加载···";
    titleLabel.font = [UIFont systemFontOfSize:12];
    
    [loadingView addSubview:loadingAnimationView];
    [loadingView addSubview:titleLabel];
    
    return loadingView;
}

// 加载通用动画
+ (UIImageView *)loadingAnimationViewWithFrame:(CGRect)frame
{
    UIImageView *view = [[UIImageView alloc] initWithFrame:frame];
    view.backgroundColor = [UIColor clearColor];
    view.animationImages = [self loadingAnimationImageArray];
    view.animationDuration = 3; // 动画时间
    [view startAnimating];
    
    return view;
}



// 加载动画帧
+ (NSArray *)loadingAnimationImageArray
{
    NSMutableArray *imgArray = [NSMutableArray arrayWithCapacity:20];
    //    for (int i = 1; i < 20; i ++) {
    //        UIImage *img = Img(StrWithFormat(@"mj_loading_%d",i));
    //        [imgArray addObject:img];
    //    }
    
    for (int i=1; i<=10; i++) {
        UIImage *img = Img(StrWithFormat(@"%d",i));
        [imgArray addObject:img];
    }
    
    return imgArray;
}


@end
